public class PageView {
//	private long totalrecordnumber;
//	private int currentpage;
//	private int totalpagenumber;
	private boolean previndex=false;
	private boolean firstindex=false;
	private int startindex = 0;
	private int endindex = 0;
	private boolean lastindex=false;
	private boolean nextindex=false;
	public PageView() {	}
//	public PageView(long totalrecordnumber,int currentpage,int maximum,int viewperpage){
//		this.totalrecordnumber=totalrecordnumber;
//		this.currentpage=currentpage;
//		this.totalpagenumber=(int) (totalrecordnumber%maximum==0?totalrecordnumber/maximum : totalrecordnumber/maximum+1);
//		setIndex(currentpage,viewperpage,totalpagenumber);
//	}
//	public long getTotalrecordnumber() {
//		return totalrecordnumber;
//	}
//	public int getCurrentpage() {
//		return currentpage;
//	}
//	public int getTotalpagenumber() {
//		return totalpagenumber;
//	}
	public boolean isPrevindex() {
		return previndex;
	}
	public boolean isFirstindex() {
		return firstindex;
	}
	public int getStartindex() {
		return startindex;
	}
	public int getEndindex() {
		return endindex;
	}
	public boolean isLastindex() {
		return lastindex;
	}
	public boolean isNextindex() {
		return nextindex;
	}
	public void setIndex(int totalpage, int viewpage, int currentpage) {
		/*
		startindex = currentpage - (viewpage % 2 == 0 ? viewpage / 2 - 1 : viewpage / 2);
		endindex = currentpage + viewpage / 2;
		if (startindex < 1) {
			startindex = 1;
			if (totalpage >= viewpage)
				endindex = viewpage;
			else
				endindex = totalpage;
		}
		if (endindex > totalpage) {
			endindex = totalpage;
			if ((endindex - viewpage) > 0)
				startindex = endindex - viewpage + 1;
			else
				startindex = 1;
		}
		 */

		if (viewpage >= totalpage) {
			startindex = 1;
			endindex = totalpage;
		} else {
			if (currentpage <= viewpage / 2) {
				startindex = 1;
				endindex = viewpage;
			} else if ((currentpage + viewpage / 2) > totalpage) {
				startindex = totalpage - viewpage + 1;
				endindex = totalpage;
			} else {
				startindex = currentpage - (viewpage - 1) / 2;
				endindex = currentpage + viewpage / 2;
			}
		}
		if(currentpage>1){
			previndex=true;
			if(startindex>1){
				firstindex=true;
			}
		}
		if(currentpage<totalpage){
			nextindex=true;
			if(endindex<totalpage){
				lastindex=true;
			}
		}
		print();
	}

	private void print() {
		if(previndex){
			System.out.print("前一页 ");
		}
		if(firstindex){
			System.out.print("首页 ");
		}
		for (int i = startindex; i <= endindex; i++) {
			System.out.print(i + " ");
		}
		if(lastindex){
			System.out.print("尾页 ");
		}
		if(nextindex){
			System.out.print("下一页 ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		new PageView().setIndex(5, 10, 1);
		new PageView().setIndex(5, 10, 2);
		new PageView().setIndex(20, 10, 2);
		new PageView().setIndex(20, 10, 5);
		new PageView().setIndex(20, 10, 6);
		new PageView().setIndex(20, 10, 11);
		new PageView().setIndex(20, 10, 17);
		new PageView().setIndex(20, 10, 20);
		
		new PageView().setIndex(20, 11, 1);
		new PageView().setIndex(20, 11, 2);
		new PageView().setIndex(20, 11, 5);
		new PageView().setIndex(20, 11, 6);
		new PageView().setIndex(20, 11, 11);
		new PageView().setIndex(20, 11, 17);
		new PageView().setIndex(20, 11, 20);
	}
}
